// pages/edit-person/index.js
import config from '../../etc/config.js'
const {
  $Toast
} = require('../../components/dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '姓名',
    placeholder_text: '',
    name: '',
    cardNumber: '',
    phone: '',
    value: '',
    sexs: [
      { name: '男', checked: 1 },
      { name: '女', checked: 0 }
    ],
    disabled: false
  },

// 修改用户信息
  submitEdit(e) {
    this.setData({
      disabled:true
    })
    let params = {
      userId: wx.getStorageSync('userId'),
    }
    let token = wx.getStorageSync('token')
    // 修改昵称
    if (this.data.name) {
      params.userName = this.data.value
      config._post('/user/updateName', token, params).then(res => {
        this.setData({
          disabled: false
        })
        if(res.code == 0){
          wx.navigateBack()
        }else{
          $Toast({
            content: res.msg
          })
        }
      })
    }
    // 修改性别
    if (this.data.sex) {
      params.userSex = this.data.value
      config._post('/user/updateSex', token, params).then(res => {
        this.setData({
          disabled: false
        })
        if (res.code == 0) {
          wx.navigateBack()
        } else {
          $Toast({
            content: res.msg
          })
        }
      })
    }
    // 修改电话
    if (this.data.phone) {
      params.userId = wx.getStorageSync('userId'),
      params.dePhone = this.data.value
      config._post('/deliveryman/updateDelivermanPhone', token, params).then(res => {
        console.log('fffffffffffff',res)
        this.setData({
          disabled: false
        })
        if (res.code == 0) {
          wx.navigateBack()
        } else {
          $Toast({
            content: res.msg
          })
        }
      })
    }
    // 修改会员卡号
    if (this.data.cardNumber) {
      params.userCardNumber = this.data.value
      config._post('/user/updateUserMember', token, params).then(res => {
        this.setData({
          disabled: false
        })
        if (res.code == 0) {
          wx.navigateBack()
        } else {
          $Toast({
            content: res.msg
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.hideShareMenu()
    this.setData({
      title: options.title,
      placeholder_text: '请输入' + options.title
    })
    if (options.name) {
      this.setData({
        name: options.name,
      })
      this.data.value = options.name
    }
    if (options.sex) {
      this.setData({
        sex: options.sex == 1 ? '男' : '女',
      })
      this.data.value = options.sex
    }
    if (options.phone) {
      this.setData({
        phone: options.phone,
      })
      this.data.value = this.data.phone
    }
    if (options.cardNumber) {
      this.setData({
        cardNumber: options.cardNumber,
      })
      this.data.value = this.data.cardNumber
    }
  },

  valChange(e) {
    this.data.value = e.detail.detail.value
  },

  bindChange(e){
    this.setData({sex: this.data.sexs[e.detail.value].name})
    this.data.value = this.data.sexs[e.detail.value].checked
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})